<template>
  <el-dialog title="题目预览" :visible.sync="value.dialogFormVisible" @close="hCloseVideo">
    <!-- 题目基本信息 -->
    <el-row class="row_preview">
      <el-col :span="6">【题型】：
        <span v-if="previewData.questionType === '1'">单选</span>
        <span v-else-if="previewData.questionType === '2'">多选</span>
        <span v-else-if="previewData.questionType === '3'">简答</span>
      </el-col>
      <el-col :span="6">【编号】：{{ previewData.id }}</el-col>
      <el-col :span="6">【难度】：
        <span v-if="previewData.difficulty === '1'">简单</span>
        <span v-else-if="previewData.difficulty === '2'">一般</span>
        <span v-else-if="previewData.difficulty === '3'">困难</span>
      </el-col>
      <el-col :span="6">【标签】：{{ previewData.tags }}</el-col>
    </el-row>
    <el-row class="row_preview">
      <el-col :span="6">【学科】：{{ previewData.subject }}</el-col>
      <el-col :span="6">【目录】：{{ previewData.directory }}</el-col>
      <el-col :span="6">【方向】：{{ previewData.direction }}</el-col>
    </el-row>
    <el-divider></el-divider>

    <!-- 题干 -->
    <el-row class="row_preview">【题干】：</el-row>
    <el-row class="row_preview">
      <p class="question">{{ previewData.question | html2Text}}</p>
    </el-row>
    <!-- 题干内容 -->
    <div v-if="previewData.questionType !== '3'">
      <el-row class="row_preview">
        <span v-if="previewData.questionType === '1'">单选题</span>
        <span v-else-if="previewData.questionType === '2'">多选题</span>
        选项：（以下选中的选项为正确答案）
      </el-row>
      <!-- 单选 -->
      <div v-if="previewData.questionType === '1'">
        <el-radio-group :value="1">
          <el-row class="row_preview" v-for="option in options" :key="option.id">
            <el-radio
              :label="option.isRight">
              {{option.title}}</el-radio>
          </el-row>
        </el-radio-group>
      </div>
      <!-- 多选 -->
      <div v-else-if="previewData.questionType === '2'">
        <el-checkbox-group :value="[1]">
          <el-row class="row_preview" v-for="option in options" :key="option.id">
            <el-checkbox
              :label="option.isRight">
              {{option.title}}</el-checkbox>
          </el-row>
        </el-checkbox-group>
      </div>
    </div>

    <el-divider></el-divider>
    <!-- 参考答案 -->
    <el-row class="row_preview">【参考答案】：
      <el-button type="danger" @click="isPlayChange">视频答案预览</el-button>
    </el-row>
    <!-- 视频播放 -->
    <el-row class="row_preview" v-if="previewData.isPlay">
      <video
        muted
        width="500px"
        :src="previewData.videoURL"
        controls="controls"
        autoplay="autoplay"></video>
    </el-row>
    <el-divider></el-divider>
    <!-- 答案解析 -->
    <el-row class="row_preview">【答案解析】：{{ previewData.answer | html2Text }}</el-row>
    <el-divider></el-divider>
    <!-- 题目备注 -->
    <el-row>【题目备注】：{{ previewData.remarks | html2Text }}</el-row>
    <!-- 关闭 -->
    <div slot="footer" class="dialog-footer">
      <!-- TODO:关闭时候，清空播放器 -->
      <el-button type="primary" @click="hCloseVideo">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { detail } from '@/api/hmmm/questions'
export default {
  name: 'questions-preview',
  props: ['value'], // 传过来的是一个对象
  data () {
    return {
      dialogVisible: this.value.dialogFormVisible, // 控制对话框是否显示
      quesId: this.value.questionId, // 父组件传过来的id
      previewData: { // 预览-区域
        questionType: null, // 预览-题型
        id: null, // 预览-编号
        difficulty: null, // 预览-难度
        tags: null, // 预览-标签
        number: null, // 预览-试题编号
        subject: null, // 预览-学科
        directory: null, // 预览-目录
        direction: null, // 预览-方向
        question: null, // 预览-题干
        answer: '无', // 预览-答案解析
        videoURL: null, // 预览-视频地址
        isPlay: false, // 预览-控制视频播放器显示
        remarks: '无' // 预览-题目备注
      },
      options: [] // 预览-题目选项
    }
  },
  watch: {
    // 监听传过来的id变化
    'value.questionId': {
      // handler是固定函数
      handler (newVal) {
        // 渲染弹出框
        this.preQuestion(newVal)
      },
      // 传过来的props值是对象，所以需要深度监听
      deep: true
    }
  },
  methods: {
    // 1.预览出现弹出框
    async preQuestion (questionId) {
      // 重新打开的时候清空上一个预览对话框
      this.previewData.videoURL = null
      this.previewData.isPlay = false
      // 打开对话框
      this.dialogFormVisible = true
      try {
        const { data: res } = await detail({ id: questionId })
        // 第一,二行数据
        this.previewData.questionType = res.questionType
        this.previewData.id = res.id
        this.previewData.difficulty = res.difficulty
        this.previewData.tags = res.tags
        this.previewData.number = res.number
        this.previewData.subject = res.subjectName
        this.previewData.directory = res.directoryName
        this.previewData.direction = res.direction
        // 题干
        this.previewData.question = res.question
        // 选项(单选,多选)
        this.options = res.options.map(item => {
          return { isRight: item.isRight, title: item.title }
        })
        // 参考答案
        this.previewData.videoURL = res.videoURL
        // 答案解析
        this.previewData.answer = res.answer ? res.answer : '无'
        // 题目备注
        this.previewData.remarks = res.remarks ? res.remarks : '无'
      } catch {
        this.$message.error('请求失败，请检查接口！')
      }
    },
    // 2.是否出现视频播放器
    isPlayChange () {
      if (!this.previewData.videoURL) {
        return this.$message.error('暂无视频源！')
      }
      this.previewData.isPlay = true
    },
    // 3.关闭
    hCloseVideo () {
      // 关闭弹窗
      this.value.dialogFormVisible = false
      // 视频窗口关闭
      this.previewData.isPlay = false
    }
  }
}
</script>

<style scoped>
  /* 预览 */
  .row_preview {
    margin-bottom: 10px;
  }
   /* 题干 */
  .question {
    color: #0f0fff;
  }
</style>
